//
//  NSObject+SCRequest.m
//  BOSS-SOOC
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 Gcx. All rights reserved.
//

#import "NSObject+SCRequest.h"

#import <objc/runtime.h>
static void *VcHTTPClient_;
//static NSInteger _conflictLoginCode = 99;
static NSInteger _failLoginCode = 2;
static NSInteger _reLoginCode = 1;

@implementation NSObject (SCRequest)

- (void)setHTTPClient:(SCHttpManager *)HTTPClient {
    objc_setAssociatedObject(self, &VcHTTPClient_, HTTPClient, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (SCHttpManager *)HTTPClient {
    if (!VcHTTPClient_) {
        self.HTTPClient = [[SCHttpManager alloc] init];
    }
    return objc_getAssociatedObject(self, &VcHTTPClient_);
}


#pragma mark - 网络请求
/**
 * 请求数据的方法
 *
 * @param req SCHttpRequest实体类
 * @param startBlock 开始请求时需要做的处理
 * @param successBlock 请求成功后需要做的处理
 * @param failureBlock 请求失败后需要做的处理
 * @param finishBlock 请求结束后需要做的处理
 */
- (void)doPOSTConnect:(SCHttpRequest *)req
                start:(HttpConnectStartBlock)startBlock
              success:(HttpConnectSuccessBlock)successBlock
              failure:(HttpConnectFailureBlock)failureBlock
               finish:(HttpConnectFinishBlock)finishBlock{
    
    [self.HTTPClient doHTTPPost:req
                          start:^(SCHttpRequest *req) {
                              if (startBlock) {
                                                            
                                  startBlock(req);
                              }
                          }
                        success:^(SCHttpRequest *req, SCHttpResponse *resp) {
                            NSLog(@"%@",resp.originalDictionary);
                                
                                successBlock(req,resp);
                        }
                        failure:^(SCHttpRequest *req, NSError *error) {
                            if (failureBlock) {
                                failureBlock(req,error);
                            }
                        }
                         finish:^(SCHttpRequest *req, BOOL success) {
                             if (finishBlock) {
                                 finishBlock(req,success);
                             }
                         }];
    
}

- (void)doGetConnect:(SCHttpRequest *)req
                start:(HttpConnectStartBlock)startBlock
              success:(HttpConnectSuccessBlock)successBlock
              failure:(HttpConnectFailureBlock)failureBlock
               finish:(HttpConnectFinishBlock)finishBlock{
    
    [self.HTTPClient doHTTPGet:req
                          start:^(SCHttpRequest *req) {
                              if (startBlock) {
                                  
                                  startBlock(req);
                              }
                          }
                        success:^(SCHttpRequest *req, SCHttpResponse *resp) {
                            NSLog(@"%@",resp.originalDictionary);
                            
                            successBlock(req,resp);
                        }
                        failure:^(SCHttpRequest *req, NSError *error) {
                            if (failureBlock) {
                                failureBlock(req,error);
                            }
                        }
                         finish:^(SCHttpRequest *req, BOOL success) {
                             if (finishBlock) {
                                 finishBlock(req,success);
                             }
                         }];
    
}


@end
